
import "package:flutter_keep_book/core/extensions/map.dart";

import "user_info.dart";

class UserModel {
  UserModel();

  UserInfo? userInfo;
  StoreInfo? storeInfo;
  UserOrderInfo? orderInfo;

  static UserModel? fromJson(dynamic json) {
    if (json is! Map || json.isEmpty) {
      return null;
    }

    return UserModel()
      ..userInfo = UserInfo.fromJson(json['userInfo'])
      ..storeInfo = StoreInfo.fromJson(json['storeInfo'])
      ..orderInfo = UserOrderInfo.fromJson(json['orderInfo']);
  }

  Map<String, dynamic> toJson() {
    final json = <String, dynamic>{};

    final userInfo = this.userInfo;
    if (userInfo != null) {
      json['userInfo'] = userInfo.toJson();
    }

    final storeInfo = this.storeInfo;
    if (storeInfo != null) {
      json['storeInfo'] = storeInfo.toJson();
    }

    final orderInfo = this.userInfo;
    if (orderInfo != null) {
      json['orderInfo'] = orderInfo.toJson();
    }
    return json;
  }
}

class StoreInfo {
  StoreInfo();

  int goodsCount = 0;
  int mbCount = 0;

  static StoreInfo? fromJson(dynamic json) {
    if (json is! Map || json.isEmpty) {
      return null;
    }

    return StoreInfo()
      ..goodsCount = json.intValue('goods_count') ?? 0
      ..mbCount = json.intValue('mb_count') ?? 0;
  }

  Map<String, dynamic> toJson() => <String, dynamic>{
        'goods_count': goodsCount,
        'mb_count': mbCount,
      };
}

class UserOrderInfo {
  UserOrderInfo();

  int delivery = 0;
  int received = 0;

  static UserOrderInfo? fromJson(dynamic json) {
    if (json is! Map || json.isEmpty) {
      return null;
    }

    return UserOrderInfo()
      ..delivery = json.intValue('delivery') ?? 0
      ..received = json.intValue('received') ?? 0;
  }

  Map<String, dynamic> toJson() => <String, dynamic>{
        'delivery': delivery,
        'received': received,
      };
}
